#更改JSON文件中的内容
import json
import argparse
from pathlib import Path

#判断源路径和目标路径是否存在
def judgeOriginTarget(origin_file_path):
    if not origin_file_path.exists():
        print("you file is not exist")
        exit()

def Parse_Arguments():
    parser = argparse.ArgumentParser(description="")
    parser.add_argument('--json-path', default=r"D:\tempDownload\dataset\elevator\seg\img09-19", type=str)
    return parser.parse_args()

def main():
    args = Parse_Arguments()
    origin_file_path = Path(args.json_path)
    judgeOriginTarget(origin_file_path)
    for file in origin_file_path.glob("*.json"):
        with open(file) as f:
            content = json.load(f)
        # image_path = content["imagePath"].split("/")[-1]
        # content["imagePath"] = image_path
        # print(content)
        # with open(file, 'w') as f:
        #     json.dump(content,f)
        shapes = content["shapes"]
        for i, shape in enumerate(shapes):
            if shape["label"] == "dog":
                shape["label"] = "pet"
                shapes[i] = shape
        content["shapes"] = shapes
        with open(file, 'w') as f:
            json.dump(content,f)


if __name__ == "__main__":
    main()
